package com.keur.money.ui.user.act

import android.view.View
import androidx.recyclerview.widget.DefaultItemAnimator
import androidx.recyclerview.widget.LinearLayoutManager
import com.keur.money.R
import com.keur.money.base.adapter.MullaOnItemChildClickListener
import com.keur.money.base.utils.TitleBarUtil
import com.keur.money.databinding.ActMullaUserPointsBinding
import com.keur.money.http.HttpHelper
import com.keur.money.mvp.MvpAct
import com.keur.money.ui.user.adapter.KeurUserPointsAdapter
import com.keur.money.ui.user.impl.KeurUserPointsPresenterImpl
import com.keur.money.ui.user.view.IKeurUserPointsView
import com.keur.money.utils.event.KeurEventData
import com.keur.money.utils.event.KeurEventType
import com.keur.money.utils.helper.KeurCustomConfigsHelper
import org.json.JSONObject

/**
 * MullaFlow
 * @Author wwdeng
 * @CreateTime: 2024/8/12 16:07
 * @Annotation：用户积分列表
 */
class KeurUserPointsAct: MvpAct<IKeurUserPointsView, KeurUserPointsPresenterImpl>(),IKeurUserPointsView {
    lateinit var binding: ActMullaUserPointsBinding

    var page = 1

    private var pointsAdapter: KeurUserPointsAdapter? = null
    override fun createPresenter(): KeurUserPointsPresenterImpl {
        return KeurUserPointsPresenterImpl(this)
    }

    override fun getMullaRootView(): View {
        binding = ActMullaUserPointsBinding.inflate(layoutInflater)
        return binding.root
    }

    override fun initMullaView() {
        TitleBarUtil.setStatusBarLightMode(this, false)
        setMullaTitleBarVisibility(View.GONE)
        binding.contentLayout.setPadding(0, TitleBarUtil.getStatusBarHeight(),0,0)
        initMullaRefreshLayout()
        initMullaRecyclerView()

        binding.textPoints.text = getMullaUserPoints().toString()

        //牌照信息
        val licenseInfo = KeurCustomConfigsHelper.getMullaLicenseInfo()
        if (licenseInfo == null){
            binding.licenseLayout.removeAllViews()
            binding.licenseLayout.visibility = View.GONE
        } else {
            binding.licenseLayout.removeAllViews()
            binding.licenseLayout.addView(licenseView())
            binding.licenseLayout.visibility = View.VISIBLE
        }
    }

    override fun initMullaListener() {
        binding.imgBack.setOnClickListener {
            finish()
        }
    }

    override fun initMullaData() {
        showMullaLoading(isTOuchOut = false, isCancel = true)
        onMullaRefreshAction()
    }


    /* 初始化刷新 */
    private fun initMullaRefreshLayout() {
        //禁止加载
        binding.refreshLayout.setEnableLoadMore(true)
        binding.refreshLayout.setEnableAutoLoadMore(false)
        binding.refreshLayout.setOnRefreshListener {
            onMullaRefreshAction()
        }
        binding.refreshLayout.setOnLoadMoreListener {
            onMullaLoadMoreAction()
        }
    }

    /**
     * 初始化积分明细列表
     */
    private fun initMullaRecyclerView() {
//        binding.recyclerView.setHasFixedSize(true)
//        binding.recyclerView.isNestedScrollingEnabled = false
//        binding.recyclerView.isFocusableInTouchMode = false //不需要焦点
//        val topDecoration = DensityUtils.dp2px(10f)
//        binding.recyclerView.addItemDecoration(LinearSpacingItemDecoration(0,0,topDecoration,0))
        binding.recyclerView.layoutManager = LinearLayoutManager(this)
        pointsAdapter = KeurUserPointsAdapter(binding.recyclerView)
        binding.recyclerView.adapter = pointsAdapter
        binding.recyclerView.itemAnimator = DefaultItemAnimator()
        pointsAdapter?.setOnItemChildClickListener(MullaOnItemChildClickListener { _, childView, position ->
            val item = pointsAdapter?.getItem(position)
            when (childView.id) {
                R.id.itemLayout -> {//点击Item

                }
            }
        })
    }

    /**
     * 刷新
     */
    private fun onMullaRefreshAction(){
        page = 1
        presenter.loadMullaUserPointsListAction()
    }

    /**
     * 加载更多
     */
    private fun onMullaLoadMoreAction(){
        page++
        presenter.loadMullaUserPointsListAction()
    }

    override fun loadMullaPage(): Int {
        return page
    }

    /**
     * 设置积分明细
     */
    override fun showMullaUserPointsList(totalCount: Int, pointsList: ArrayList<JSONObject>) {
        if (page == 1){
            pointsAdapter?.data = pointsList
        } else {
            pointsAdapter?.addMoreData(pointsList)
        }
        pointsAdapter?.let {
            if (it.itemCount >= totalCount){
                binding.refreshLayout.finishLoadMoreWithNoMoreData()
            } else {
                binding.refreshLayout.resetNoMoreData()
            }
        }
    }

    override fun hideMullaLoading() {
        super.hideMullaLoading()
        binding.refreshLayout.finishRefresh()
        binding.refreshLayout.finishLoadMore()
    }

    override fun onKeurEvent(event: KeurEventData) {
        //刷新积分完成
        if (event.eventType == KeurEventData.TYPE.KEUR_POINTS_REFRESHED){
            binding.textPoints.text = getMullaUserPoints().toString()
        }
    }

    override fun onStart() {
        super.onStart()
        HttpHelper.addKeurEventAction(KeurEventType.PAGE.KEUR_PAGE_POINTS_LIST_START)
    }

    override fun onDestroy() {
        super.onDestroy()
        HttpHelper.addKeurEventAction(KeurEventType.PAGE.KEUR_PAGE_POINTS_LIST_END)
    }
}